<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use App\Models\ResourceCollect;
use App\Models\ResourceReadRecom;
use App\Models\ResourceReadRecomBook;
use App\Validate\ResourceReadRecomBookValidate;

/**
 * 推荐阅读（书单形式数据）书籍列表
 */
class ResourceReadRecomController extends Controller
{
    public $model = null;
    public $bookModel = null;
    public $validate = null;

    public function __construct()
    {
        parent::__construct();

        $this->model = new ResourceReadRecom();
        $this->bookModel = new ResourceReadRecomBook();
        $this->validate = new ResourceReadRecomBookValidate();
    }
    /**
     * 获取推荐阅读（书单形式数据）每日荐读列表
     * @param keywords 检索数据
     * @param page 页数    默认第一页
     * @param limit 限制条数   默认 10 条
     * @param correct_read_id 当前每日荐书的id，如果传了则表示是获取往前荐书  不传获取全部
     */
    public function getResourceReadRecomList()
    {
        $keywords = $this->request->keywords;
        $correct_read_id = $this->request->correct_read_id;
        $page = intval($this->request->input('page', 1)) ?: 1;
        $limit = intval($this->request->input('limit', 10)) ?: 10;
        $is_need_book_info = $this->request->is_need_book_info;

        //以资源商显示数据
        $res = $this->model->lists(['keywords' => $keywords, 'is_play' => 1, 'correct_read_id' => $correct_read_id, 'is_need_book_info' => $is_need_book_info, 'limit' => $limit]);
        if ($res['data']) {
            if ($is_need_book_info) {
                $field = ['r.id', 'r.read_id', 'r.resource_id', 'guid', 'title', 'img'];
                foreach ($res['data'] as $key => $val) {
                    $book_list = $this->bookModel->lists(['read_id' => $val['id'], 'field' => $field, 'limit' => 99]);
                    $res['data'][$key]['book_list'] = !empty($book_list) ? $book_list['data'] : [];
                }
            }
            $res = $this->disPageData($res);
            return $this->returnApi(200, '获取成功', true, $res);
        }
        return $this->returnApi(203, '暂无数据');
    }

    /**
     * 获取推荐阅读（书单形式数据）详情
     * @param read_id  每日荐读id
     * @param is_need_book_info 是否同时返回书籍信息  1 是 其余不返回
     */
    public function getResourceReadRecomDetail()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('wx_get_resource_read_recom_detail')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }
        $user_id = $this->request->user_info['id'];
        $read_id = $this->request->read_id;
        $is_need_book_info = $this->request->is_need_book_info;
        //以资源商显示数据
        $res = $this->model->detail($read_id);
        if ($res) {
            $number = $this->model->where('is_del', 1)->where('is_play', 1)->count();
            if ($number > 1) {
                $limit = 99;
                $res['is_more'] = false;
            } else {
                $limit = 5;
                $res['is_more'] = true; //存在更多
            }
            if ($is_need_book_info) {
                $book_list = $this->bookModel->lists(['read_id' => $read_id, 'limit' => $limit]);
                foreach ($book_list['data'] as $key => &$val) {
                    if ($user_id) {
                        //判断是否收藏
                        $is_collect = ResourceCollect::isCollect($val['guid'], $user_id);
                        $val['is_collect'] = $is_collect ? true : false;
                    } else {
                        $val['is_collect'] = false; //未登录，默认未收藏
                    }
                }
                $res['book_list'] = !empty($book_list) ? $book_list['data'] : [];
            }
            return $this->returnApi(200, '获取成功', true, $res);
        }
        return $this->returnApi(203, '暂无数据');
    }


    /**
     * 获取推荐阅读（书单形式数据）书籍列表
     * @param read_id  每日荐读id
     * @param big_type_id 大类id
     * @param page 页数    默认第一页
     * @param limit 限制条数   默认 10 条
     */
    public function getResourceReadRecomBookList()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('wx_get_resource_read_recom_book_list')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        $read_id = $this->request->read_id;
        $big_type_id = $this->request->big_type_id;
        $page = intval($this->request->input('page', 1)) ?: 1;
        $limit = intval($this->request->input('limit', 10)) ?: 10;

        //以资源商显示数据
        $res = $this->bookModel->lists(['read_id' => $read_id, 'big_type_id' => $big_type_id, 'limit' => $limit]);
        if ($res['data']) {
            $res = $this->disPageData($res);
            return $this->returnApi(200, '获取成功', true, $res);
        }
        return $this->returnApi(203, '暂无数据');
    }
}
